//点击返回顶部
let scrolltop = document.documentElement.scrollTop
$("#foot ul .li3").click(function () {
    $("html,body").animate({ scrollTop: 0 }, 500)
})
//点击图标返回首页
$("#header img").click(function(){
    window.location.href="../index.html"
})
//处理url
let reg = /([^&=]+)=?([^&]*)/g
let str = decodeURI(window.location.href)
console.log(str);
//???????????????????????????????????????????????????
var categoryid = parseInt(reg.exec(str)[2])
var cname = reg.exec(str)[2]
var brandTitleId = reg.exec(str)[2]-0
console.log(categoryid);
console.log(cname);
console.log(brandTitleId);
var brandName = cname
//???????????????????????????????????????????????????
//更改分类标签
fetch(`http://chst.vip:1234/api/getproduct?productid=${categoryid}`)
    .then(body => body.json())
    .then(res => {
        console.log(res);
        let result = res.result
        console.log(result);
        //category: "数码相机"
        //categoryId: 11
        //titleId: 1
        let pname = result[0].productName
        let reg = /([^\s]+)\s.*/
        pname=pname.replace(reg, "$1")
        //匹配出第一个空格前的内容
        let str = ""
        str=`
        <div class="tag">
            <a href="" class="puba a1">首页</a> &gt; 
            <a href="" class="puba a2" categoryid="${categoryid}">${cname}</a> &gt; 
            <a href="" class="puba a3">${pname}</a>
            <p class="screen">筛选</p>
        </div>
        `
        $("#tag").html(str)
        //点击之前的十大类排行lis页
        $("#tag .tag .a2").attr("href",`ppdq_class.html?brandTitleId=${brandTitleId}&brandName=${brandName}`)
        $("#tag .tag .a1").attr("href","../index.html")
    })
//渲染商品detail
fetch(`http://chst.vip:1234/api/getproduct?productid=${categoryid}`)
.then(body=>body.json())
.then(res=>{
    let obj=res.result[0]
    console.log(obj);
    //取图片地址
    let adrress = obj.productImg
    let reg=/^<[^<]+\s+src="([^''""]*)"/
    adrress=reg.exec(adrress)[1]
    let str=`
    <li>
    <img src="${adrress}" alt="" class="img1">
    <p class="jieshao">${obj.productName}</p>
    ${obj.bjShop}
    </li>
    `
    $(".lis").html(str)
})
//渲染沙雕网友评论
fetch(`http://chst.vip:1234/api/getproductcom?productid=${categoryid}`)
.then(body=>body.json())
.then(res=>{
    let arr = res.result
    let str =""
    arr.forEach(item => {
        str+=`
    <div class="box">
        <div class="title">
            <p class="j">${item.comName}</p>
            <p class="j">${item.comTime}</p>
        </div>
        <div class="container">
        ${item.comContent}
        </div>
    </div>                
        `
    });
    $(".wypj").html(str)
})
